//
//  GRMyGRiceViewController.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/7/26.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRMyGRiceViewController.h"
#import "GRRegisterViewController.h"

#import "GRMyRiceCommonHeader.h"
#import "GRMyCommonViewCell.h"
#import "IntegralGiveViewController.h"
#import "IntegralDetailViewController.h"
@interface GRMyGRiceViewController ()

{
    NSArray *titleArray;
    BOOL safePwd;
}
@property (nonatomic, strong) GRMyRiceCommonHeader *header;
@property(nonatomic,copy)NSString * viewPoint;

@end

@implementation GRMyGRiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的龟米";
    
    titleArray = @[@"龟米互赠", @"交易密码", @"交易记录"];
    // Uncomment the following line to preserve selection between presentations.
    self.tableView.tableHeaderView = self.header;
    self.tableView.rowHeight       = 50;
    self.tableView.tableFooterView = [UIView new];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    [self.tableView registerNib:[UINib nibWithNibName:@"GRMyCommonViewCell" bundle:nil] forCellReuseIdentifier:@"GRMyCommonViewCell"];
    
    [self getMyRice];
}


/**
 获取龟米信息
 */
- (void)getMyRice {
    [GRPersonAction getMyRiceInfomation:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            safePwd        = [responseObject[@"safe"] boolValue];
            self.header.myRice.text = [NSString stringWithFormat:@"%.2f",[responseObject[@"viewPoint"] floatValue]];
            self.viewPoint = responseObject[@"viewPoint"];
            [self.tableView reloadData];
        }else {
            [GRHUD showMessage:[error errorMsg] atView:self.view];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return titleArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GRMyCommonViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GRMyCommonViewCell" forIndexPath:indexPath];
    cell.titleLabel.text     = titleArray[indexPath.row];
    if (indexPath.row == 1) {
        cell.pwdStatus.text  = safePwd ? @"修改密码" : @"未设置";
    }else {
        cell.pwdStatus.text  = nil;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:{
            IntegralGiveViewController * giveVC = [[IntegralGiveViewController alloc]init];
            giveVC.tortoiseRice = self.viewPoint;
            giveVC.isPW = safePwd;
            [self.navigationController pushViewController:giveVC animated:YES];
        }
            break;
        
        case 1:{
            GRRegisterViewController *registerVc = [[GRRegisterViewController alloc] init];
            registerVc.registerMode = safePwd ? GRRegisterModeModifyTradePassword : GRRegisterModeSetTradePassword;
            registerVc.finishBlock = ^{
                [self getMyRice];
            };
            [self.navigationController pushViewController:registerVc animated:YES];
        }
            break;

        default:{
            IntegralDetailViewController * intergralDetailVC = [[IntegralDetailViewController alloc]init];
            [self.navigationController pushViewController:intergralDetailVC animated:YES];
            
            
        }
            break;
    }
}




/**
 懒加载

 @return <#return value description#>
 */
-(GRMyRiceCommonHeader *)header {
    if (!_header) {
        _header = [GRMyRiceCommonHeader loadFromNib];
        [_header configureRice:YES];
    }
    return _header;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
